package com.ywh.composetest.widget

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.text.buildSpannedString
import com.ywh.composetest.clickableNoRipple


@Preview(backgroundColor = 0xffffff)
@Composable
fun TextTestCompose() {
    val shape = RoundedCornerShape(10.dp)
    Text(
        text = "Text基本使用Text基本使用Text基本使用",
        modifier = Modifier
            .width(180.dp)
            .wrapContentHeight()
            .clip(shape)
            .background(Color.Green)
            .border(1.dp, Color.Red, shape)
            .clickableNoRipple {
                Log.e("ComposeText", "click")
            }
            .padding(horizontal = 10.dp, vertical = 3.dp),
        textAlign = TextAlign.End,//对齐方式
        color = Color.Blue,//颜色
        fontSize = 12.sp,//大小
        fontStyle = FontStyle.Normal,//字体
        fontWeight = FontWeight.Bold,//字重
        letterSpacing = 3.sp,//字横向间距
        textDecoration = TextDecoration.Underline, //删除线，下划线
        overflow = TextOverflow.Ellipsis,
        softWrap = true, //遇到换行符或者超过容器宽度自动换行
        minLines = 1, maxLines = 1,
        style = TextStyle(
            color = Color.Red,
            fontSize = 20.sp,
            fontStyle = FontStyle.Italic
        ),//Text中的属性会覆盖style里面的属性，比如同时设置color和style里面的color,会以Text的color属性优先
        onTextLayout = {//文本布局完成后回调
            Log.e("Text", "onTextLayout")
        }
    )
}


@Preview(backgroundColor = 0xffffff)
@Composable
fun TextAnnotatedStringCompose() {
    val text = "类似于原生的Span点我"
    val annotatedText = remember {
        buildAnnotatedString {
            withStyle(style = SpanStyle(color = Color.Blue, fontSize = 20.sp)) {
                append("类似于")
            }
            append("原生的")
            withStyle(
                style = SpanStyle(
                    color = Color.Green,
                    fontSize = 12.sp,
                    fontStyle = FontStyle.Italic
                )
            ) {
                append("Span")
            }
            withStyle(
                style = SpanStyle(
                    color = Color.Red,
                    fontSize = 12.sp,
                    fontStyle = FontStyle.Italic,
                    textDecoration = TextDecoration.Underline
                )
            ) {
                append("点我")
            }
        }
    }
    Box(modifier = Modifier.background(Color.White)) {
        ClickableText(text = annotatedText) { offset ->  //自己算位置处理片段点击
            val linkStart = text.indexOf("点我")
            val linkEnd = linkStart + 2
            if (offset in linkStart..<linkEnd) {
                Log.e("ClickableText", "点我")
            }
        }
    }
}